package Client;

import Message.*;

import java.awt.event.*;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.Timestamp;
import java.util.Date;

public class ChatUIActionListener implements ActionListener, KeyListener,WindowListener {

    ChatUI chatUI;
    AccountSendMessageInfo accountSendMessageInfo;


    ChatUIActionListener(ChatUI chatUI) {
        this.chatUI = chatUI;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == chatUI.bSend) {
            if (!chatUI.areaInputText.getText().equals("")) {
                String info = chatUI.areaInputText.getText().trim();
                accountSendMessageInfo=new AccountSendMessageInfo(chatUI.user.getUserId(),chatUI.getChatObject(),info);
                try{
                    Socket socket = new Socket(ServerInfo.SERVER_IP_ADDRESS, ServerInfo.SERVER_ACCOUNTSENDMESSAGEINFO_PORT);
                    OutputStream outputStream = socket.getOutputStream();
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
                    objectOutputStream.writeObject(accountSendMessageInfo);
                    //获得服务器的回应。
                    InputStream inputStream = socket.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    String res = null;
                    res = bufferedReader.readLine();

                    if (res.equals("true")) {
                        System.out.println("成功添加聊天记录到服务端");
                    } else {
                        System.out.println("添加聊天记录到服务端失败");
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

                //chatUI.areaShowChatInfo.append(new Timestamp(System.currentTimeMillis()) + "\r\n" + info + "\r\n");
                chatUI.areaInputText.setText("");

            }
        }
    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_ENTER) {//ctrl+entel发送非空消息
            if (!chatUI.areaInputText.getText().equals("")) {
                String info = chatUI.areaInputText.getText();

                accountSendMessageInfo=new AccountSendMessageInfo(chatUI.user.getUserId(),chatUI.getChatObject(),info);
                try{
                    Socket socket = new Socket(ServerInfo.SERVER_IP_ADDRESS, ServerInfo.SERVER_ACCOUNTSENDMESSAGEINFO_PORT);
                    OutputStream outputStream = socket.getOutputStream();
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
                    objectOutputStream.writeObject(accountSendMessageInfo);
                    //获得服务器的回应。
                    InputStream inputStream = socket.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                    String res = null;
                    res = bufferedReader.readLine();

                    if (res.equals("true")) {
                        System.out.println("成功添加聊天记录到服务端");
                    } else {
                        System.out.println("添加聊天记录到服务端失败");
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

                //chatUI.areaShowChatInfo.append(new Timestamp(System.currentTimeMillis()) + "\r\n" + info + "\r\n");
                chatUI.areaInputText.setText("");
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }


    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {

    }

    @Override
    public void windowClosed(WindowEvent e) {
        this.chatUI.user.removeChatUIFromChatUIList(this.chatUI.getChatUIId());//聊天窗口关闭则从用户聊天列表删除
    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}
